package com.da.javatest.leetCode2;

public class Code415 {
    public static void main(String[] args) {
        System.out.println(new Code415().addStrings("98", "9"));
    }

    public String addStrings(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        char[] chars1 = num1.toCharArray();
        char[] chars2 = num2.toCharArray();
        int l1 = num1.length() - 1;
        int l2 = num2.length() - 1;
        boolean flag = false;
        for (; Math.max(l1, l2) >= 0; l1--, l2--) {
            int i1, i2;
            if (l1 < 0) {
                i1 = 0;
            } else {
                i1 = Integer.parseInt(chars1[l1] + "");
            }
            if (l2 < 0) {
                i2 = 0;
            } else {
                i2 = Integer.parseInt(chars2[l2] + "");
            }
            int total = i1 + i2;
            if (flag) {
                total++;
                flag = false;
            }
            if (total > 9) {
                total -= 10;
                flag = true;
            }
            sb.insert(0, total);
        }
        if (flag) {
            sb.insert(0, 1);
        }
        return sb.toString();
    }
}
